package com.autumn.mybatis.criterion;

import java.util.List;

import com.autumn.mybatis.mapper.IEntityMapper;
import com.autumn.util.ExceptionUtils;

/**
 * 实体查询
 * 
 * @author 杨昌国 2018-12-07 17:24:43
 */
public class EntityQuery<TEntity> extends Query {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8718018301318951571L;

	private final Class<TEntity> entityClass;

	/**
	 * 实例化 EntityQuery 类新实例
	 * 
	 * @param entityClass 实体类型
	 */
	public EntityQuery(Class<TEntity> entityClass) {
		super(entityClass);
		this.entityClass = ExceptionUtils.checkNotNull(entityClass, "entityClass");
	}

	/**
	 * 获取实体类型
	 * 
	 * @return
	 */
	public final Class<TEntity> getEntityClass() {
		return entityClass;
	}

	/**
	 * 基于条件删除
	 * 
	 * @param repository 仓储
	 * @return 返回影响行数
	 */
	public int deleteByWhere(IEntityMapper<TEntity> repository) {
		ExceptionUtils.checkNotNull(repository, "repository");
		return repository.deleteByWhere(this.builderSection());
	}

	/**
	 * 查询实体首条记录
	 * 
	 * @param repository 仓储
	 * @return
	 */
	public TEntity selectByFirst(IEntityMapper<TEntity> repository) {
		ExceptionUtils.checkNotNull(repository, "repository");
		return repository.selectFirst(this.builderSection());
	}

	/**
	 * 查询list集合
	 * 
	 * @param repository 仓储
	 * @return
	 */
	public List<TEntity> selectByList(IEntityMapper<TEntity> repository) {
		ExceptionUtils.checkNotNull(repository, "repository");
		return repository.selectList(this.builderSection());
	}

	/**
	 * 查询记录数
	 * 
	 * @param repository 仓储
	 * @return
	 */
	public int countByWhere(IEntityMapper<TEntity> repository) {
		ExceptionUtils.checkNotNull(repository, "repository");
		return repository.countByWhere(this.builderSection());
	}

	/**
	 * 是否存在记录
	 * 
	 * @param repository 仓储
	 * @return
	 */
	public boolean exist(IEntityMapper<TEntity> repository) {
		return this.countByWhere(repository) > 0;
	}
}
